package com.sx.micowell.framework.model.smartDevice;

import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.example.mvvm.base.data.SingleLiveEvent;
import com.example.mvvm.base.vm.BaseViewModel;
import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.ToastUtils;
import com.sx.micowell.framework.binding.BindingCommand;
import com.sx.micowell.framework.binding.impl.IBindingAction;
import com.tuya.smart.android.user.api.IRegisterCallback;
import com.tuya.smart.android.user.api.IResetPasswordCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IResultCallback;

/**
 * @Author: YY
 * @Time: 2021/9/16
 * @Describe TODO
 */
public class SmartDeviceRegisterModel extends BaseViewModel {

    // 1 注册 3 重置密码
    public int mRegisterType = 1;

    // 手机号码
    public ObservableField<String> phoneNumber = new ObservableField<>("");

    //验证码
    public ObservableField<String> verifyCode = new ObservableField<>("");

    //设置密码
    public ObservableField<String> setPassword = new ObservableField<>("");

    //发送验证码
    public SingleLiveEvent<Void> sendCodeEvent = new SingleLiveEvent<>();

    //获取验证码点击事件
    public BindingCommand<IBindingAction> getCodeOnClickCommand = new BindingCommand<>(() -> {
        if (TextUtils.isEmpty(phoneNumber.get())) {
            ToastUtils.showShort("手机号码不能为空");
            return;
        }
        // Get verification code code by phone or Email
        TuyaHomeSdk.getUserInstance().sendVerifyCodeWithUserName(
                phoneNumber.get(),
                "",
                "86",
                mRegisterType,
                new IResultCallback() {

                    @Override
                    public void onSuccess() {
                        ToastUtils.showLong("您将收到来自【智能生活】的短信验证, 请查收");
                        sendCodeEvent.call();
                    }

                    @Override
                    public void onError(String code, String error) {
                        ToastUtils.showShort(error);
                    }
                });
    });

    //点击注册按钮事件
    public BindingCommand<IBindingAction> registerOnClickCommand = new BindingCommand<>(() -> {
        if (TextUtils.isEmpty(phoneNumber.get())) {
            ToastUtils.showShort("手机号码不能为空");
            return;
        }

        if (TextUtils.isEmpty(verifyCode.get())) {
            ToastUtils.showShort("验证码不能为空");
            return;
        }

        if (TextUtils.isEmpty(setPassword.get())) {
            ToastUtils.showShort("密码不能为空");
            return;
        }

        if (mRegisterType == 1) {
            TuyaHomeSdk.getUserInstance().registerAccountWithPhone(
                    "86",
                    phoneNumber.get(),
                    setPassword.get(),
                    verifyCode.get(),
                    new IRegisterCallback() {
                        @Override
                        public void onSuccess(User user) {
                            ToastUtils.showShort("注册成功");
                        }

                        @Override
                        public void onError(String code, String error) {
                            ToastUtils.showShort("注册失败" + error);
                        }
                    }
            );

        } else {
            // Reset phone password
            TuyaHomeSdk.getUserInstance().resetPhonePassword(
                    "86",
                    phoneNumber.get(),
                    verifyCode.get(),
                    setPassword.get(),

                    new IResetPasswordCallback() {
                        @Override
                        public void onSuccess() {
                            ToastUtils.showShort("重置密码成功");
                        }

                        @Override
                        public void onError(String code, String error) {
                            ToastUtils.showShort("重置密码失败" + error);
                        }
                    }
            );
        }

    });
}
